#ifndef __TAB_CONTROL_H__
#define __TAB_CONTROL_H__

#ifdef __apple__
#include "cxx_0x.h"
#include "Key.h"
#include "Container.h"
#include "Layout.h"
#include "DockStyle.h"
#include "Brush.h"
#else
#include "gofgame/common/cxx_0x.h"
#include "gofgame/common/ui/Key.h"
#include "gofgame/common/ui/Container.h"
#include "gofgame/common/ui/Layout.h"
#include "gofgame/common/ui/DockStyle.h"
#include "gofgame/common/graphics/Brush.h"
#endif //__apple__

#include <map>

namespace gofgame {
	namespace common {
		namespace ui {

			using namespace gofgame::common::ui;
			using namespace gofgame::common::graphics;
			using namespace std;

			class TabPage;

			class TabControl : public Container {
			public:
				TabControl();
				virtual ~TabControl();
				
				virtual void setLayout(Layout *layout) _override _final;

				virtual Brush *getTabButtonBrush() const;
				virtual void setTabButtonBrush(Brush *tabButtonBrush);
				virtual Brush *getTabButtonBackgroundBrush() const;
				virtual void setTabButtonBackgroundBrush(Brush *tabButtonBackgroundBrush);
				virtual const char *getTabButtonFontName() const;
				virtual void setTabButtonFontName(const char *tabButtonFontName);
				virtual float getTabButtonFontSize() const;
				virtual void setTabButtonFontSize(float tabButtonFontSize);
				virtual Brush *getSelectedTabButtonBrush() const;
				virtual void setSelectedTabButtonBrush(Brush *selectedTabButtonBrush);
				virtual Brush *getSelectedTabButtonBackgroundBrush() const;
				virtual void setSelectedTabButtonBackgroundBrush(Brush *selectedTabButtonBackgroundBrush);
				virtual const char *getSelectedTabButtonFontName() const;
				virtual void setSelectedTabButtonFontName(const char *tabButtonFontName);
				virtual float getSelectedTabButtonFontSize() const;
				virtual void setSelectedTabButtonFontSize(float tabButtonFontSize);

				virtual int getSelectedIndex() const;
				virtual void setSelectedIndex(int index);

				virtual float getCardDistance() const;
				virtual void setCardDistance(float cardDistance);
				virtual DockStyle getCardDockStyle() const;
				virtual void setCardDockStyle(DockStyle cardDockStyle);

				virtual void addChild(Component *child) _override _final;
				virtual void addChild(Component *child, int zOrder) _override _final;
				virtual void addChild(Component *child, int zOrder, int tag) _override _final;

				virtual void addChild(TabPage *child);
				virtual void addChild(TabPage *child, int zOrder);
				virtual void addChild(TabPage *child, int zOrder, int tag);

			protected:
				virtual void onTouchEnded(TouchEventArgs &e) _override;
				virtual void drawAfterChildren() _override;
				virtual void drawCardsBackground();
				virtual void drawCardsBaseLine();
				virtual void drawTabButton(TabPage *tabPage, const CCRect& tabButtonRect);
				CCObject *getTabPageData(TabPage *tabPage, Key &key);
				void setTabPageData(TabPage *tabPage, Key &key, CCObject *tabPageData);
				void clearUnnecessaryTabPageData();
				CCRect getCardsBound(float actualDistance = 0);

			private:
				float cardDistance;
				DockStyle cardDockStyle;
				map<TabPage*, map<Key*, Ref<CCObject>>*> tabPageData;
				Ref<Brush> tabButtonBrush;
				Ref<Brush> tabButtonBackgroundBrush;
				string tabButtonFontName;
				float tabButtonFontSize;
				Ref<Brush> selectedTabButtonBrush;
				Ref<Brush> selectedTabButtonBackgroundBrush;
				string selectedTabButtonFontName;
				float selectedTabButtonFontSize;

				static Key KEY_TAB_BUTTON_TEXTURE;
				static Key KEY_SELECTED_TAB_BUTTON_TEXTURE;

			private:
				void getCardBounds(CCRect *arr);
				map<Key*, Ref<CCObject>> *getDataMap(TabPage *tabPage, bool create);
				class TabLayout : public Layout {
				protected:
					virtual void onLayout() _override;
				};
			};
		}
	}
}

#endif //__TAB_CONTROL_H__
